/*
 * DogeMenuFrame.java
 *
 * Created on 15 luglio 2007, 13.03
 */

package dogeUI;

import dogeCore.*;
import java.sql.SQLException;

/**
 *
 * @author  daniele
 */
public class DogeMenuFrame extends javax.swing.JPanel {
    
    /**
     * Creates new form DogeMenuFrame
     */
    public DogeMenuFrame(DogeCore core, DogeMainWindow ui) {
        this.core=core;
        this.ui=ui;
        this.mode=NO_MODE;
        initComponents();
        //set username and buttons that have to be visible according to his permissions
        try {
            usernameLabel.setText(core.getUsername());
            setPermissions();
        } catch (UserNotLoggedException ex) {
            ex.printStackTrace();
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        menuGroup = new javax.swing.ButtonGroup();
        usernameLabel = new javax.swing.JLabel();
        logoutButton = new javax.swing.JButton();
        insertButton = new javax.swing.JToggleButton();
        editButton = new javax.swing.JToggleButton();
        searchButton = new javax.swing.JToggleButton();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        usernameLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        usernameLabel.setText("[username]");

        logoutButton.setFont(new java.awt.Font("Dialog", 1, 14));
        logoutButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dogeUI/images/exit.gif")));
        logoutButton.setText("Esci");
        logoutButton.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        logoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutButtonActionPerformed(evt);
            }
        });

        menuGroup.add(insertButton);
        insertButton.setFont(new java.awt.Font("Dialog", 1, 14));
        insertButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dogeUI/images/filenew.gif")));
        insertButton.setText("Inserisci");
        insertButton.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        insertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertButtonActionPerformed(evt);
            }
        });

        menuGroup.add(editButton);
        editButton.setFont(new java.awt.Font("Dialog", 1, 14));
        editButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dogeUI/images/edit.gif")));
        editButton.setText("Modifica");
        editButton.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        menuGroup.add(searchButton);
        searchButton.setFont(new java.awt.Font("Dialog", 1, 14));
        searchButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dogeUI/images/filefind.gif")));
        searchButton.setText("Ricerca e visualizza");
        searchButton.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(usernameLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(logoutButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(insertButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(editButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(searchButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(usernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logoutButton)
                .addGap(37, 37, 37)
                .addComponent(insertButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchButton)
                .addGap(316, 316, 316))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        showSearchPanel();
    }//GEN-LAST:event_searchButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        showEditPanel();
    }//GEN-LAST:event_editButtonActionPerformed

    private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertButtonActionPerformed
        showInsertPanel();
    }//GEN-LAST:event_insertButtonActionPerformed

    private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutButtonActionPerformed
        doLogout();
    }//GEN-LAST:event_logoutButtonActionPerformed

    /** enables or disables controls according to current user permissions */
    private void setPermissions() {
        setInsertAllowed(core.insertAllowed());
        setEditAllowed(core.editAllowed());
        setSearchAllowed(core.searchAllowed());
    }

    /** enables or disables insert button */
    private void setInsertAllowed(boolean b) {
        insertButton.setEnabled(b);
    }
    
    /** enables or disables edit button 
     *
     *  in this version a Java edit interface is not available, so the button is disabled and hidden by default
     */
    private void setEditAllowed(boolean b) {
        /* COMMENT THE FOLLOWING TWO LINES AND UNCOMMENT THE THIRD TO OBTAIN *REAL* BEHAVIOUR OF THIS BUTTON */
        editButton.setEnabled(false);
        editButton.setVisible(false);      
        //editButton.setEnabled(b);       
    }
    
    /** enables or disables search button
     *
     *  in this version a Java search interface is not available, so the button is disabled and hidden by default
     */
    private void setSearchAllowed(boolean b) {
        /* COMMENT THE FOLLOWING TWO LINES AND UNCOMMENT THE THIRD TO OBTAIN *REAL* BEHAVIOUR OF THIS BUTTON */
        searchButton.setEnabled(false);
        searchButton.setVisible(false);      
        //searchButton.setEnabled(b);
    }
    
    /** call DogeCore to log out and informs main window to show again login form */
    private void doLogout(){
        try{
            //call core to log out
            core.doLogout();
            //show a nel login frame
            ui.newLogin();
        } catch (SQLException ex){}
    }
    
    /** tells main window to show insert panel */
    private void showInsertPanel() {
        if (mode!=INSERT_MODE){
            mode=INSERT_MODE;
            ui.showInsertPanel();
        }
    }
    /** tells main window to show edit panel */
    private void showEditPanel() {
        if (mode!=EDIT_MODE){
            mode=EDIT_MODE;
            ui.showEditPanel();
        }
    }
    /** tells main window to show search panel */
    private void showSearchPanel() {
        if (mode!=SEARCH_MODE)
            mode=SEARCH_MODE;
            ui.showSearchPanel();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton editButton;
    private javax.swing.JToggleButton insertButton;
    private javax.swing.JButton logoutButton;
    private javax.swing.ButtonGroup menuGroup;
    private javax.swing.JToggleButton searchButton;
    private javax.swing.JLabel usernameLabel;
    // End of variables declaration//GEN-END:variables
    //core of DoGe
    private DogeCore core;
    //window that contains this menu
    private DogeMainWindow ui;
    //mode field to prevent reloading of current mode panel when clicking again on an already selected menu item...
    private byte mode;
    //...and related constants
    private static final byte NO_MODE=0;
    private static final byte INSERT_MODE=1;
    private static final byte EDIT_MODE=2;
    private static final byte SEARCH_MODE=3;
}
